﻿package mortal.game.utils
{
    import Message.Public.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.view.mainUI.roleAvatar.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class BuffUtil extends Object
    {
        public static const ControlWalk:int = 1;
        public static const ControlAttack:int = 2;
        public static const ControlReleaseMagic:int = 4;
        public static const Invincible:int = 1024;
        private static var notEffectDic:Dictionary;
        private static var ignoreToNotEffect:Dictionary;

        public function BuffUtil()
        {
            return;
        }// end function

        private static function buildNotEffect() : void
        {
            notEffectDic = new Dictionary();
            notEffectDic[EBuffType._EBuffTypeSleepy] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypeDizzy] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypeConfused] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypeFreeze] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypeScared] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypePetrified] = ControlWalk | ControlAttack | ControlReleaseMagic;
            notEffectDic[EBuffType._EBuffTypeHoldStill] = ControlWalk;
            notEffectDic[EBuffType._EBuffTypeBanSkill] = ControlReleaseMagic;
            return;
        }// end function

        private static function buildIgnoreToNotEffect() : void
        {
            ignoreToNotEffect = new Dictionary();
            ignoreToNotEffect[EBuffType._EBuffTypeSleepy] = EBuffEffect._EBuffEffectSleep;
            ignoreToNotEffect[EBuffType._EBuffTypeDizzy] = EBuffEffect._EBuffEffectDizzy;
            ignoreToNotEffect[EBuffType._EBuffTypeConfused] = EBuffEffect._EBuffEffectConfusion;
            ignoreToNotEffect[EBuffType._EBuffTypeHoldStill] = EBuffEffect._EBuffEffectHoldStill;
            ignoreToNotEffect[EBuffType._EBuffTypeBanSkill] = EBuffEffect._EBuffEffectSilence;
            ignoreToNotEffect[EBuffType._EBuffTypeFreeze] = EBuffEffect._EBuffEffectFreeze;
            ignoreToNotEffect[EBuffType._EBuffTypeScared] = EBuffEffect._EBuffEffectScared;
            return;
        }// end function

        public static function effect(param1:int, param2:int) : Boolean
        {
            if (!notEffectDic)
            {
                buildNotEffect();
            }
            return (notEffectDic[param1] & param2) != 0;
        }// end function

        public static function checkBuffIgnoreEffect(param1:Array, param2:int, param3:int) : Boolean
        {
            var _loc_5:BuffData = null;
            if (!ignoreToNotEffect)
            {
                buildIgnoreToNotEffect();
            }
            var _loc_4:Array = [];
            for each (_loc_5 in param1)
            {
                
                if (_loc_5.tbuff.type == EBuffType._EBuffTypeInvincible)
                {
                    return true;
                }
            }
            for each (_loc_5 in param1)
            {
                
                if ((ignoreToNotEffect[_loc_5.tbuff.type] & param2) != 0)
                {
                    continue;
                }
                _loc_4.push(_loc_5);
            }
            for each (_loc_5 in _loc_4)
            {
                
                if (effect(_loc_5.tbuff.type, param3))
                {
                    return false;
                }
            }
            return true;
        }// end function

        public static function isCanRoleWalk() : Boolean
        {
            return checkBuffIgnoreEffect(Cache.instance.buff.buffInfoArray, 0, ControlWalk);
        }// end function

        public static function isCanRoleFireSkill(param1:SkillInfo) : Boolean
        {
            return checkBuffIgnoreEffect(Cache.instance.buff.buffInfoArray, param1.tSkill.ignoreBuff, ControlReleaseMagic);
        }// end function

        public static function isCanRoleAttack() : Boolean
        {
            return checkBuffIgnoreEffect(Cache.instance.buff.buffInfoArray, 0, ControlAttack);
        }// end function

        public static function addBuffByCode(... args) : void
        {
            var _loc_3:SBuff = null;
            var _loc_4:int = 0;
            args = new SBuffUpdate();
            args.op = EBuffUpdateType._EBuffUpdateTypeAdd;
            args.buffs = new Array();
            for each (_loc_4 in args)
            {
                
                _loc_3 = new SBuff();
                _loc_3.buffId = _loc_4;
                args.buffs.push(_loc_3);
            }
            Cache.instance.buff.updateBuff(args);
            NetDispatcher.dispatchCmd(ServerCommand.BufferUpdate, Cache.instance.buff.showBuffArray);
            return;
        }// end function

        public static function removeBuffByCode(... args) : void
        {
            var _loc_3:SBuff = null;
            var _loc_4:int = 0;
            args = new SBuffUpdate();
            args.op = EBuffUpdateType._EBuffUpdateTypeRemove;
            args.buffs = new Array();
            for each (_loc_4 in args)
            {
                
                _loc_3 = new SBuff();
                _loc_3.buffId = _loc_4;
                args.buffs.push(_loc_3);
            }
            Cache.instance.buff.updateBuff(args);
            NetDispatcher.dispatchCmd(ServerCommand.BufferUpdate, Cache.instance.buff.showBuffArray);
            return;
        }// end function

    }
}
